<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Users;
use App\Model\Roles;
use Input;
use App\Tools\UploadFiles;

class UserController extends CommonController
{
	//列表
    public function index(Request $request){
    	$users = new Users;
    	$data['userCount'] = $users->count();//获取总用户数目
    	$data['userAll'] = $users->orderBy('id','desc')->paginate(10);//所有用户列表
    	return view('admin.user_list',$data);
    }

    //增加
    public function add(Request $request){
    	$roles = new Roles;
    	$data['roleAll'] = $roles->get(); //获取所有角色

    	// 判断是否post
    	if($request->isMethod('post')){
    		$arr = Input::only('username','password','nick','gender','email','integral','money','roles','is_admin');
    		$arr['password'] = bcrypt($arr['password']);
    		$arr['reg_time'] = time();

    		$uploads = new UploadFiles;//实例化上传类
    		$is_upload = $uploads->uploads('file','80','80',1);
    		if(empty($is_upload)){
    			$arr['avatar'] = $is_upload['smallPath'];
    		}

    		$users = new Users;
    		$users->insert($arr);
    		return redirect('/Admin/User/add');
    	}
    	return view('admin.user_add',$data);
    }

    //编辑
    public function edit(Request $request,$id){
    	$users = new Users;
    	$roles = new Roles;
    	$data['roleAll'] = $roles->get(); //获取所有角色
    	$data['userInfo'] = $users->find($id);

    	// 判断是否post
    	if($request->isMethod('post')){
    		$arr = Input::only('password','nick','gender','email','integral','money','roles','is_admin');
    		if(!empty($arr['password'])){
    			$arr['password'] = bcrypt($arr['password']);
    		}else{
    			unset($arr['password']);
    		}

    		$uploads = new UploadFiles;//实例化上传类
    		$is_upload = $uploads->uploads('file','80','80',1);
    		if(empty($is_upload)){
    			$arr['avatar'] = $is_upload['smallPath'];
    		}

    		$users->where('id',$id)->update($arr);
    		return redirect('/Admin/User/list');
    	}
    	return view('admin.user_edit',$data);
    }


    // 删除
    public function del($id){
    	$users = new Users;
    	$users->where('id',$id)->delete($id);
    	return redirect('/Admin/User/list');
    }


}
